﻿//首次载入事件
$(function () {
    f_GetChart();
});

var windowheight = $(window).height();//可视区域高度
if (windowheight <= 800) {
    $("#chartMain").css("height", "350px");
}
else {
    $("#chartMain").css("height", "450px");
}

$("div[ana_tabclick]").find("a").click(function () {
    $(this).siblings("a").removeClass("on");
    $(this).addClass("on");
    f_GetChart();
});

function f_Chart(chartType, chartTitle, dataSource) {
    var chartxAxis = "[";
    var chartSeries = "[";
    for (var i = 0; i < dataSource.length; i++) {
        chartxAxis += "'" + dataSource[i].description + "'";
        chartSeries += dataSource[i].count;
        if (i < dataSource.length - 1) {
            chartxAxis += ",";
            chartSeries += ",";
        }
    }
    chartxAxis += "]";
    chartSeries += "]";

    chartxAxis = eval(chartxAxis);
    chartSeries = eval(chartSeries);
    /*面积图*/
    var myChart11 = echarts.init(document.getElementById("chartMain"), theme2);
    switch (chartType) {
        case "l":
            option = {
                //title: {
                //    text: "" + chartTitle + ""
                //},
                tooltip: {
                    trigger: 'axis'
                },
                grid : {
                    y2 : 50
                },
                //legend: {
                //    data: ['再次到访', '首次到访', '到访总量'],
                //    x: 5,
                //    y: 0,
                //	show:false
                //},
                xAxis: [
                    {
                        type: 'category',
                        axisLabel: {
                            interval: 0,
                            rotate: 30,
                        },
                        boundaryGap: false,
                        //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                        data: chartxAxis
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                draggable: false,
                //animation: false,
                calculable: false,
                tooltip: {
                    show: false
                },
                series: [
                    {
                        //name: '到访总量',
                        type: "line",
                        smooth: true,
                        itemStyle: {
                            normal: {
                                areaStyle: { type: 'default' },
                                label: {
                                    show: true,
                                    textStyle: {
                                        fontSize: '14',
                                        fontFamily: '微软雅黑',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        },
                        //data: [1320, 1132, 601, 234, 120, 90, 20]
                        data: chartSeries
                    }
                ]
            };
            break;
        case "c":
        case "l":
            option = {
                //title: {
                //    text: "" + chartTitle + ""
                //},
                tooltip: {
                    trigger: 'axis'
                },
				grid : {
                    y2 : 50
                },
                //legend: {
                //    data: ['再次到访', '首次到访', '到访总量'],
                //    x: 5,
                //    y: 0,
                //	show:false
                //},
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
						axisLabel: {
                            interval: 0,
                            rotate: 30,
                        },
                        //data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                        data: chartxAxis
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                draggable: false,
                //animation: false,
                calculable: false,
                tooltip: {
                    show: false
                },
                series: [
                    {
                        //name: '到访总量',
                        type: "bar",
                        smooth: true,
                        barWidth: 10,
                        barCategoryGap: 15,
                        barGap: 10,
                        itemStyle: {
                            normal: {
                                areaStyle: { type: 'default' },
                                label: {
                                    show: true,
                                    textStyle: {
                                        fontSize: '14',
                                        fontFamily: '微软雅黑',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        },
                        //data: [1320, 1132, 601, 234, 120, 90, 20]
                        data: chartSeries
                    }
                ]
            };
            break;
    }

    myChart11.setOption(option);

}

//日期 开始时间    结束时间
var StartDate; var EndDate;

function f_DateRange(startTime, endTime) {
    $("#div_date,#div_compare_date").remove();
    var str = "<div class=\"date\" id=\"div_date\"><span class=\"date_title\" id=\"date\"></span><a class=\"opt_sel\" id=\"input_trigger\"><i class=\"i_orderd\"></i></a></div>";
    $(".analysis_time").append(str);
    var dateRange = new pickerDateRange('date', {
        startDate: startTime, // startDate 开始时间
        endDate: endTime, // endDate 结束时间
        theme: 'ta',
        target: 'asdfasdf', // target 日期选择框摆放的位置, 一般是在 form 中
        success: function (obj) {
            $(".ana_time").find("a").each(function () {
                $(this).removeClass("on");
            });
            f_GetChart();
        }
    });
}

function f_DaysNum(startTime, endTime) {
    var start_time = new Date(startTime).getTime();
    var end_time = new Date(endTime).getTime();
    var sys_second = (end_time - start_time) / 1000;
    if (sys_second > 0) {
        return Math.floor((sys_second / 3600) / 24) + 1;
    }
    return 1;
};


//获取数据
//function f_GetChart() {

//    var date = new Date();
//    var dateType = $("#SelectDate").find("a.on").attr("name");//日期类型
//    var sytleType = $("#StyleType").find("a.on").attr("name");//风格
//    //更新日期控件时间
//    dateType = dateType == undefined ? "" : dateType;
//    EndDate = date.format('yyyy-MM-dd');
//    switch (dateType) {
//        case "d":
//            StartDate = change_date(-1);
//            $("#date").html("" + StartDate + " 至 " + EndDate + "");
//            break;
//        case "w":
//            StartDate = change_date(-7);
//            $("#date").html("" + StartDate + " 至 " + EndDate + "");
//            break;
//        case "m":
//            StartDate = change_date(-30);
//            $("#date").html("" + StartDate + " 至 " + EndDate + "");
//            break;
//        case "y":
//            StartDate = change_date(-365);
//            $("#date").html("" + StartDate + " 至 " + EndDate + "");
//            break;
//    }
//    //重新获取时间
//    var selectDate = $("#date").html();
//    if (selectDate != undefined) {
//        StartDate = selectDate.split(' 至 ')[0];
//        EndDate = selectDate.split(' 至 ')[1];
//    }
//    f_DateRange(StartDate, EndDate);

//    var daysNum = f_DaysNum(StartDate, EndDate);
//    if (daysNum == 1 || daysNum == 7 || daysNum == 30 || daysNum==365) {
//        f_GetData(sytleType, daysNum);
//    } else {
//        f_GetData(sytleType,0,StartDate,EndDate);
//    }    
//}

function f_GetChart() {

    var date = new Date();
    var dateType = $("#SelectDate").find("a.on").attr("name");//日期类型
    var sytleType = $("#StyleType").find("a.on").attr("name");//风格
    //更新日期控件时间
    dateType = dateType == undefined ? "" : dateType;
    EndDate = date.format('yyyy-MM-dd');
    switch (dateType) {
        case "d":
            $("#date").html("" + EndDate + " 至 " + EndDate + "");
            break;
        case "w":
            $("#date").html("" + GetWeekStartDate() + " 至 " + GetWeekEndDate() + "");
            break;
        case "m":
            $("#date").html("" + GetMonthStartDate() + " 至 " + GetMonthEndDate() + "");
            break;
        case "y":
            $("#date").html("" + GetYearStartDate() + " 至 " + GetYearEndDate() + "");
            break;
    }
    //重新获取时间
    var selectDate = $("#date").html();
    if (selectDate != undefined) {
        StartDate = selectDate.split(' 至 ')[0];
        EndDate = selectDate.split(' 至 ')[1];
    }
    f_DateRange(StartDate, EndDate);
    var daysNum = f_DaysNum(StartDate, EndDate);
    switch (dateType) {
        case "d":           
        case "w":           
        case "m":            
        case "y":
            f_GetData(sytleType, daysNum);
            break;
        default:
            f_GetData(sytleType, 0, StartDate, EndDate);
            break;
    }
}

function GetWeekStartDate()
{
    var time = new Date();
    var weekNo = time.getDay();
    if (weekNo == 0) {
        weekNo = 7;
    }
    time.setDate(time.getDate() - weekNo + 1);   
    var startime = time.format('yyyy-MM-dd');
    return startime;
}

function GetWeekEndDate() {
    var time = new Date();
    var weekNo = time.getDay();
    if (weekNo == 0) {
        weekNo = 7;
    }
    time.setDate(time.getDate() - weekNo + 7);
    var endtime = time.format('yyyy-MM-dd');
    return endtime;
}

function GetMonthStartDate() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    var firstDay = year + '-' + month + "-" + "01";
    return firstDay;
}

function GetMonthEndDate() {
    var myDate = new Date();
    var year = myDate.getFullYear();
    var month = myDate.getMonth() + 1;
    if (month < 10) {
        month = "0" + month;
    }
    myDate = new Date(year, month, 0);
    var lastDay =year+'-'+month + "-" + myDate.getDate();
    return lastDay;
}

function GetYearStartDate()
{
    var myDate = new Date();
    var year = myDate.getFullYear();
    var startDate = year+'-01-01';
    return startDate;
}

function GetYearEndDate()
{
    var myDate = new Date();
    var year = myDate.getFullYear();
    var endDate = year+'-12-31';
    return endDate;
}
